﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WHMS.EntityClasses
{
	public partial class ShelfEntity
	{
		string _row, _column, _cell, _storeCode;

		public string Row
		{
			get
			{
				var parts = Code.Split('-');
				if (parts.Length > 1)
					return parts[1];

				return string.Empty;
			}
			set
			{
				_row = value;

				var parts = Code.Split('-');
				if (parts.Length != 4)
					SetCode();
				else
					Code = parts[0] + "-" + _row + "-" + parts[2] + "-" + parts[3];

			}
		}

		public string Column
		{
			get
			{
				var parts = Code.Split('-');
				if (parts.Length > 2)
					return parts[2];

				return string.Empty;
			}
			set
			{
				_column = value;
				
				var parts = Code.Split('-');
				if (parts.Length != 4)
					SetCode();
				else
					Code = parts[0] + "-" + parts[1] + "-" + _column + "-" + parts[3];
			}
		}

		public string Cell
		{
			get
			{
				var parts = Code.Split('-');
				if (parts.Length > 3)
					return parts[3];

				return string.Empty;
			}
			set
			{
				_cell = value;

				var parts = Code.Split('-');
				if (parts.Length != 4)
					SetCode();
				else
					Code = parts[0] + "-" + parts[1] + "-" + parts[2] + "-" + _cell;

			}
		}

		public string StoreCode
		{
			get
			{
				var parts = Code.Split('-');
				if (parts.Length > 0)
				{
					return parts[0];
				}
				else if (Store != null)
				{
					return Store.Code;
				}

				return string.Empty;
			}
			set
			{
				_storeCode = value;

				var parts = Code.Split('-');
				if (parts.Length != 4)
					SetCode();
				else
					Code = _storeCode + "-" + parts[1] + "-" + parts[2] + "-" + parts[3];

			}
		}

		private void SetCode()
		{
			Code = _storeCode + "-" + _row + "-" + _column + "-" + _cell;
		}
	}
}